<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    Cache
};
class Deploy extends Base
{
    /**
     * 配置选项
     * @return mixed
     */
    public function lists()
    {
        if(is_post()){
            $d = request()->post();
            $map = [];
            if($d['type']!='') $map[] = ['type','=',$d['type']];
            if($d['area']!='') $map[] = ['area','=',$d['area']];
            if($d['group']!='') $map[] = ['group','=',$d['group']];
            if($d['keyword']!='') $map[] = ['name|title|remark','like',"%{$d['keyword']}%"];
        	$rs = model('Deploy')::where($map)
                                ->order('sort desc,id desc')
                                ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }
        return $this->fetch();
    }
    //添加
    public function add(){
        if(is_post()){
            $d = request()->post();
            if(model('Deploy')->renew($d)){
                Cache::clear();
                session('js.function','preload');
                return suc();
            }else{
                return err(model('Deploy')->getError());
            }
        }
        return $this->fetch();
    }
    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            if(model('Deploy')->renew($d)){
                Cache::clear();
                session('js.function','preload');
                return suc();
            }else{
                return err(model('Deploy')->getError());
            }
        }
        $info = model('Deploy')::get($id);
        return $this->fetch('', ['info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = input();
            $rs = model('Deploy')->del($d['id']);
            session('js.function','reload');
            if($rs){
                return suc();
            }else{
                return err(model('Deploy')->getError());
            }
        }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
    //配置选项
    public function group($id = 1)
    {
        if(is_post()){
            $d = request()->post();
            $rs = model('Deploy')->batchSave($d);
            if($rs){
                Cache::clear();
                session('js.msg','保存成功!');
                return suc();
            }else{
                return err(model('Deploy')->getError());
            }
        }
        $data = model('Deploy')::where("group",$id)->column("*","id");
               
        return $this->fetch('', ['list' => $data,'group_id' => $id]);
    }
}
